// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import "../Components"

AndroidItem {
    id: _root
    width: 720 * viewScale
    height: 1134 * viewScale

    property string lockScreenPath: ""          //ロックスクリーン

    signal clicked(int key)
    signal unlocked()

    Image {
        anchors.fill: parent
        source: _root.lockScreenPath
        clip: true
        fillMode: Image.PreserveAspectCrop
        smooth: true
    }

    MouseArea{
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MidButton
    }

    Rectangle{
        id: _circle
        x: parent.width / 2 - width / 2
        y: (920 + 216 / 2) * viewScale - _handle.r
        width: _handle.r * 2
        height: width
        radius: width / 2
        color: "#00000000"
        opacity: 0.5
        border.color: "#ffffff"
        border.width: 2
        visible: _handle.pressed
    }

    ImageButton{
        id: _handle
        x: parent.width / 2 - width / 2
        y: 920 * viewScale
        width: 216 * viewScale
        height: 216 * viewScale
        imageIdle: "../android/images/ic_lockscreen_handle_normal.png"
        imagePress: "../android/images/ic_lockscreen_handle_pressed.png"

        onClicked: {
            _root.clicked(1);
        }
        property real r: parent.width / 2 - 100 * viewScale
        property real default_x: 0
        property real default_y: 0
        property real prev_x: 0
        property real prev_y: 0
        property bool isUnlocked: false
        property bool isHit: false      // 現状、３つのマークにあたってるか

        Component.onCompleted: {
            default_x = x;
            default_y = y;
        }

        onPressed: {
            prev_x = mouse.x;
            prev_y = mouse.y;
            _funcItemsAnim.stop();
            _funcItems.opacity = 1.0;
        }
        onReleased: {
            x = default_x;
            y = default_y;

            if(isUnlocked === true){
                unlocked();
            }

            _funcItemsAnim.start();
        }
        onMousePositionChanged: {
            if(pressed){
                var tx = x + mouse.x - prev_x;
                var ty = y + mouse.y - prev_y;
                if(checkInCircle(tx, ty) === true){
                    x += mouse.x - prev_x;
                    y += mouse.y - prev_y;
                }else{
                    x = calcOutCircleX(tx, ty);
                    y = calcOutCircleY(tx, ty);
                }

                tx = x + width / 2;
                ty = y + height / 2;
                if(checkHit(_unlock, tx, ty, isHit) === true){
                    x = _unlock.x;
                    y = _unlock.y;
                    isUnlocked = true;
                    isHit = true;
                }else{
                    if(checkHit(_camera, tx, ty, isHit) === true){
                        x = _camera.x;
                        y = _camera.y;
                        isHit = true;
                    }else if(checkHit(_silent, tx, ty, isHit) === true){
                        x = _silent.x;
                        y = _silent.y;
                        isHit = true;
                    }else{
                        isHit = true;
                    }
                    isUnlocked = false;
                }
            }
        }

        //
        function calcOutCircleX(x, y){
            var dx = x - default_x;
            var dy = y - default_y;
            var rad = Math.atan2(dy, dx);
            return r * Math.cos(rad) + default_x;
        }
        function calcOutCircleY(x, y){
            var dx = x - default_x;
            var dy = y - default_y;
            var rad = Math.atan2(dy, dx);
            return r * Math.sin(rad) + default_y;
        }

        //特定の範囲にいるか
        function checkInCircle(x, y){
            var ret = false;
            if(Math.pow(x - default_x, 2) + Math.pow(y - default_y, 2) < Math.pow(r, 2)){
                //範囲内
                ret = true;
            }
            return ret;
        }

        function checkHit(img, x, y, now){
            var ret = false;
            var adj = now ? 10 : 0;
            if((img.x + adj) < x && x < (img.x + img.width - adj)
                    && (img.y + adj) < y && y < (img.y + img.height - adj)){
                ret = true;
            }
            return ret;
        }
    }

    ImageClock{
        y: 121 * viewScale
        height: 82 * viewScale
        anchors.right: parent.right
        anchors.rightMargin: 45 * viewScale

        clockNumberWidth: height

        numberPics: new Array("images/lock_num_l_0.png"
            , "images/lock_num_l_1.png"
            , "images/lock_num_l_2.png"
            , "images/lock_num_l_3.png"
            , "images/lock_num_l_4.png"
            , "images/lock_num_l_5.png"
            , "images/lock_num_l_6.png"
            , "images/lock_num_l_7.png"
            , "images/lock_num_l_8.png"
            , "images/lock_num_l_9.png"
                    )
        colonPic: "images/lock_num_l_c.png"
    }


    Item{
        id: _funcItems
        anchors.fill: parent
        opacity: 0 //_handle.pressed ? 1.0 : 0.0
        Image{
            id: _camera
            x: _handle.default_x - _handle.r
            y: _handle.default_y
            width: 216 * viewScale
            height: 216 * viewScale
            source: "images/ic_lockscreen_camera_normal.png"
        }
        Image{
            id: _silent
            x: _handle.default_x
            y: _handle.default_y - _handle.r
            width: 216 * viewScale
            height: 216 * viewScale
            source: "images/ic_lockscreen_silent_normal.png"
        }
        Image{
            id: _unlock
            x: _handle.default_x + _handle.r
            y: _handle.default_y
            width: 216 * viewScale
            height: 216 * viewScale
            source: "images/ic_lockscreen_unlock_normal.png"
        }
    }

    NumberAnimation {
        id: _funcItemsAnim
        running: false
        target: _funcItems
        property: "opacity"
        to: 0
        duration: 1000
        easing.type: Easing.Linear
    }

    Text{
        anchors.horizontalCenter: parent.horizontalCenter
        y: 1150 * viewScale
        color: "#dddddd"
        text: "SIMカードが挿入されていません緊急通報のみ"
        font.pixelSize: 26 * viewScale
    }
}
